#define _CRT_SECURE_NO_WARNINGS 1

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */
struct ListNode* swapPairs(struct ListNode* head) {
    if (!head || !head->next)
        return head;
    struct ListNode* prev = NULL;

    struct ListNode* left = head;
    struct ListNode* right = head->next;

    head = head->next;
    while (right)
    {
        left->next = right->next;
        right->next = left;
        if (prev)
            prev->next = right;
        prev = left;

        if (left->next == NULL)
            break;
        left = left->next;
        right = left->next;
    }
    return head;
}